//app.js
import api from './utils/api.js'; 
import {Event} from './utils/Event.js';
App({
    event: new Event(),
    onLaunch: function() { 
        //this.loginApp();
        const that = this;
        // 获取系统信息
        const systemInfo = wx.getSystemInfoSync();
        //wx.setStorageSync('token',"263d51da-2fae-4eeb-86fb-4bdae7b27334")
        // 胶囊按钮位置信息
        const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
        // 导航栏高度 = 状态栏到胶囊的间距（胶囊距上距离-状态栏高度） * 2 + 胶囊高度 + 状态栏高度
        that.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
        that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
        that.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
        that.globalData.menuHeight = menuButtonInfo.height;
        //wx.setStorageSync('token',"263d51da-2fae-4eeb-86fb-4bdae7b27334")
        wx.getSystemInfo({
            success: res => {
                if (res.safeArea.top > 20) { //x及以上的异形屏top为44，非异形屏为20
                    // 可以将结果存到globalData：
                    //   console.log('shine');
                    that.globalData.isIphoneX = true
                }
            },
            fail(err) {
                // console.error(err);
            }
        })
    },
    loginApp(callback) {
        let self = this;  
        //this.loadUser(callback);
    }, 
    //加载用户
    loadUser(callback) {
        if (!this.globalData.userinfo) {
            api.userInfo({
                method: "GET",
                data: {},
                success: (ress) => {
                    // console.log(this.globalData.code);
                    const response = ress.data; 

                    if (response.code == 1 && response.data) {
                        wx.setStorageSync('token', response.data.userinfo.token)
                        this.globalData.userinfo = response.data.userinfo;
                        if (callback) {
                            callback();
                        }
                    } else {
                        wx.setStorageSync('token', null);
                    }
                }
            })
        }
    },
    globalData: {
        userInfo: null, //微信用户信息
        code: null, //微信认证的key
        userinfo: null, //系统的用户信息 
        navBarHeight: 0, // 导航栏高度
        menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
        menuBotton: 0, // 胶囊距底部间距（保持底部间距一致）
        menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致）
        isIphoneX: false, //判断iPhonex以上机型
        isLogin: true
    }
})